import cgi
import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

import xiami



class MainPage(webapp.RequestHandler):
  url=''

  def get(self,songs_info=[]):
		ifsongget=False
		if len(songs_info) >0: ifsongget=True
		template_values = {
		'url': self.url,
      		'songs_id': songs_info,
      		'ifsongget': ifsongget,      
   		}
        
    
   		path = os.path.join(os.path.dirname(__file__), 'index.html')
   		self.response.out.write(template.render(path, template_values))


  def post(self):
    self.url = self.request.get('url')
    songs_id = xiami.getSongids(self.url)
    songs_info = []
    for i in songs_id:
    	songs_info.append(xiami.getSonginfo(i))
    self.get(songs_info)

application = webapp.WSGIApplication(
                                     [('/', MainPage)
                                      ],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
